﻿using Basic.FormCollect;
using Basic.FormLocalEvent.Model;
using Basic.FormModel.DB;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.Form.Model;
using Basic.FormRemoteModel.PhysicalPage.Model;
using Basic.FormService.Interface;
using Basic.FormService.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.FormService.lmpl
{
    internal class CustomFormService : ICustomFormService
    {
        private readonly ICustomFormCollect _Form;

        private readonly IPhysicalPageCollect _PhyPage;

        public CustomFormService ( ICustomFormCollect form, IPhysicalPageCollect phyPage )
        {
            this._Form = form;
            this._PhyPage = phyPage;
        }

        public long Add ( FormAdd data )
        {
            DBCustomForm form = this._Form.Add(data);
            new FormEvent(form).AsyncSend("Add");
            return form.Id;
        }
        public FormDto Get ( long id )
        {
            DBCustomForm form = this._Form.Get(id);
            return form.ConvertMap<DBCustomForm, FormDto>();
        }

        public void Delete ( long id )
        {
            DBCustomForm form = this._Form.Get(id);
            this._Form.Delete(form);
            new FormEvent(form).AsyncSend("Delete");
        }

        public PagingResult<FormDto> Query ( FormQuery query, IBasicPage paging )
        {
            FormDto[] dtos = this._Form.Query<FormDto>(query, paging, out int count);
            return new PagingResult<FormDto>(dtos, count);
        }

        public bool Set ( long id, FormSet set )
        {
            DBCustomForm form = this._Form.Get(id);
            return this._Form.Set(form, set);
        }

        public bool SetStatus ( long id, FormStatus status )
        {
            DBCustomForm form = this._Form.Get(id);
            if ( this._Form.SetStatus(form, status) )
            {
                new FormEvent(form).AsyncSend(status == FormStatus.启用 ? "Enable" : "Stop");
                return true;
            }
            return false;
        }
        private string _GetPagePath ( long formId, PyFormPageType pageType )
        {
            PhysicalPageDto[] page = this._PhyPage.GetsByFormId<PhysicalPageDto>(formId);
            if ( page.IsNull() )
            {
                return null;
            }
            string path = page.Find(c => c.PageType == pageType, c => c.PagePath);
            return path.IsNull() ? page.Find(c => c.PageType == PyFormPageType.默认, c => c.PagePath) : path;
        }
        public FormOutline GetOutline ( long id, PyFormPageType pageType )
        {
            FormOutlineDto dto = this._Form.Get<FormOutlineDto>(id);
            if ( dto.FormType == FormType.代理表单 )
            {
                return new FormOutline
                {
                    FormName = dto.FormName,
                    FormType = dto.FormType,
                    PagePath = this._GetPagePath(id, pageType)
                };
            }
            return new FormOutline
            {
                FormName = dto.FormName,
                FormType = dto.FormType
            };
        }

        public Dictionary<long, string> GetName ( long[] ids )
        {
            return this._Form.GetName(ids);
        }

        public void LockForm ( LockFormArg arg )
        {
            if ( arg.FormId.HasValue == false || arg.LooseFormId.HasValue == false )
            {
                return;
            }
            this._Form.LockForm(arg);
        }

        public void SetLayoutType ( long id, FormLayoutType layout )
        {
            DBCustomForm form = this._Form.Get(id);
            this._Form.SetLayoutType(form, layout);
        }
    }
}
